package com.platform.modules.sys.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.EncryUtils;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.ValidatorUtils;
import com.platform.common.validator.group.AddGroup;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.enumstatus.SystemEnum;
import com.platform.common.enumstatus.UserEnum;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.entity.SysUserAptitudeEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysOrgUserService;
import com.platform.modules.sys.service.SysUserAptitudeService;
import com.platform.modules.sys.service.SysUserService;

/**
 * @author wangLc
 */
@Api(tags = "OrgRegContoller|系统企业人员管理")
@RestController
@RequestMapping("reg")
public class RegContoller extends AbstractController{
	
	@Autowired
	private SysOrgService sysOrgService;
	@Autowired
	private SysOrgUserService sysOrgUserService;
	@Autowired
	private SysUserAptitudeService sysUserAptitudeService;
	@Autowired
	private SysUserService sysUserService;

	@Value("${spring.mail.emailTile}")
	private String emailTile;
	

	@ApiOperation(value = "注册单位", notes = "注册单位")
	@SysLog("注册单位")
	@PostMapping("/saveOrg")
	public RestResponse saveOrg(SysOrgEntity sysOrg,HttpServletRequest request) {
		
		
		if(sysOrg !=null){
			 String updateUserId = "";
			 String ifUpdate = "";
        	 Map<String, Object> orgUserParams = new HashMap<String, Object>();
        	 orgUserParams.put("orgNo", sysOrg.getOrgNo());
	    	  List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(orgUserParams);
	    	  if(orgUserList != null && orgUserList.size()>0){
	    		  updateUserId = orgUserList.get(0).getUserId();
	    		  ifUpdate = "Yes";
	    		  sysOrg.setUserId(updateUserId);
	    		  sysOrg.setIfUpdate(ifUpdate);
	    	  }
			String orgNo = "";
			//所有企业账号
			if("1".equals(sysOrg.getOrgPerson()) || StringUtils.isBlank(sysOrg.getOrgPerson())){
				
				if(StringUtils.isBlank(sysOrg.getUserName())){
					return RestResponse.error("登录名不能为空");
				}
				if(StringUtils.isBlank(sysOrg.getCreditCode())){
					return RestResponse.error("统一社会信用代码不能为空");
				}
				if(StringUtils.isBlank(sysOrg.getOrgName())){
					return RestResponse.error("单位名称不能为空");
				}
				
	        	orgNo = sysOrgService.addOrg(sysOrg, null,request);
			}
			return RestResponse.success().put("orgNo", orgNo);
		}else{
			return RestResponse.error("数据错误");
		}

	}
	
	/**
	 * 重新发送邮箱
	 */
	@ApiOperation(value = "重新发送邮箱", notes = "重新发送邮箱")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userName", value = "用户登路名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "orgLinkMailbox", value = "邮箱", dataType = "String")
    })
	@SysLog("重新发送邮箱")
	@PostMapping("/sendEmailBoxAgain")
	public RestResponse sendEmailBoxAgain(String userName,String orgLinkMailbox) {
		sysOrgService.sendEmailBoxAgain(userName, orgLinkMailbox);
		return RestResponse.success();
	}
	
	/**
	 * 单位-查询注册审核结果
	 */
	@ApiOperation(value = "单位-查询注册审核结果", notes = "单位-查询注册审核结果")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "orgName", value = "单位名称", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "creditCode", value = "信用代码", dataType = "String")
    })
	@SysLog("单位-查询注册审核结果")
	@PostMapping("/displayOrgResult")
	public RestResponse displayOrgResult(String orgName,String creditCode) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("realOrgName", orgName);    //根据单位名称查询单位信息且无模糊查询
		params.put("creditCode", creditCode);
		List<SysOrgEntity> orgList = sysOrgService.queryAll(params);
		
		return RestResponse.success().put("list", orgList);
	}
	
	/**
	 * 单位发起聘用资质人员   
	 */
	@ApiOperation(value = "单位发起聘用资质人员 ", notes = "单位发起聘用资质人员 ")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "seal", value = "从事职务", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "startEmpDate", value = "聘用开始日期 yyyy-MM-dd HH:mm:ss", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "endEmpDate", value = "聘用结束日期 yyyy-MM-dd HH:mm:ss", dataType = "String")
    })
	@SysLog("单位发起聘用资质人员 ")
	@PostMapping("/orgApplyEmploy")
	public RestResponse orgApplyEmploy(String userId,String startEmpDate,String endEmpDate,String seal) {
		
		String orgNo = "";
		SysUserEntity  user =  getUser();
		//获取当前登录人所在单位Id
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());//登录人
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();
				}
			}
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orgNo", orgNo);
			params.put("userId", userId);
			//查看之前是否聘用过
			List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
			
			SysUserEntity userEntity = sysUserService.getById(userId);
			if(userEntity !=null){
				//之前聘用过
				if(orgUserList != null && orgUserList.size()>0){
					SysOrgUserEntity orgUser = orgUserList.get(0);
					orgUser.setEmployStatus(UserEnum.employStatus.FQPYDDTY.getKey());
					if(endEmpDate !=null && !"".equals(endEmpDate)){
						orgUser.setEndEmpDate(sdf.parse(endEmpDate));
					}
					if(startEmpDate != null && !"".equals(startEmpDate)){
						orgUser.setStartEmpDate(sdf.parse(startEmpDate));
					}
					orgUser.setLaunchDate(new Date());
					orgUser.setSeal(seal);
//					orgUser.setUserCard(userEntity.getUserCard());
					sysOrgUserService.updateById(orgUser);
				}else{
					SysOrgUserEntity orgUser = new SysOrgUserEntity();
					if(endEmpDate !=null && !"".equals(endEmpDate)){
						orgUser.setEndEmpDate(sdf.parse(endEmpDate));
					}
					if(startEmpDate != null && !"".equals(startEmpDate)){
						orgUser.setStartEmpDate(sdf.parse(startEmpDate));
					}
						orgUser.setUserId(userId);
						orgUser.setOrgNo(orgNo);
						orgUser.setSeal(seal);
						orgUser.setEmployStatus(UserEnum.employStatus.FQPYDDTY.getKey());
						orgUser.setLaunchDate(new Date());
						orgUser.setCreateTime(new Date());
					sysOrgUserService.save(orgUser);
				}
			}else{
				RestResponse.error("无此人数据");
			}
		
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return RestResponse.success();
	}
	
	
	/**
	 * 单位发起解聘资质人员    
	 */
	@ApiOperation(value = "单位发起解聘资质人员", notes = "单位发起解聘资质人员")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "reasonDismiss", value = "解聘原因", dataType = "String")
    })
	@SysLog("单位发起解聘资质人员")
	@PostMapping("/orgApplyDismiss")
	public RestResponse orgApplyDismiss(String userId,String reasonDismiss) {
		
		String orgNo = "";
		SysUserEntity  user =  getUser();
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());//登录人
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();
				}
			}else{
				return RestResponse.error("请检查该登陆用户是否是企业账号");
			}
		}

		 try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orgNo", orgNo);
			params.put("userId", userId);
			List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
			
			SysUserEntity userEntity = sysUserService.getById(userId);
			if(userEntity !=null){
				if(orgUserList != null && orgUserList.size()>0){
					SysOrgUserEntity orgUser = orgUserList.get(0);
					orgUser.setEmployStatus(UserEnum.employStatus.BJJPY.getKey());
					orgUser.setReasonDismiss(reasonDismiss);
					orgUser.setReasonDate(new Date());
//					orgUser.setUserCard(userEntity.getUserCard());
					sysOrgUserService.updateById(orgUser);
				}else{
					SysOrgUserEntity orgUser = new SysOrgUserEntity();
					orgUser.setUserId(userId);
					orgUser.setOrgNo(orgNo);
					orgUser.setReasonDate(new Date());
					orgUser.setReasonDismiss(reasonDismiss);
					orgUser.setEmployStatus(UserEnum.employStatus.BJJPY.getKey());
//					orgUser.setUserCard(userEntity.getUserCard());
					sysOrgUserService.save(orgUser);
				}
			}else{
				return RestResponse.error("无此人数据");
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return RestResponse.success();
	}
	
	
	/**
	 * 单位（启用/禁用）切换
	 */
	@ApiOperation(value = "单位（启用/禁用）切换", notes = "单位（启用/禁用）切换")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "orgId", value = "机构id", dataType = "String")
    })
	@SysLog("单位（启用/禁用）切换")
	@PostMapping("/updateOrgStatus")
	public RestResponse updateOrgStatus(String orgId) {
		
		SysOrgEntity org = sysOrgService.getById(orgId);
	    if(org != null){
	    	String orgStatus = org.getStatus();
	    	if(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey().equals(orgStatus)){
	    		org.setStatus(AptitudeEnum.aptitudeStatus.JY.getKey());
	    		sysOrgService.update(org);
	    		Map<String, Object> params = new HashMap<String, Object>();
	    			params.put("orgNo", org.getOrgNo());
	    			params.put("accountType", SystemEnum.accountType.ORG.getKey());
	    		List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	    		if(orgUserList != null && orgUserList.size()>0){
	    			for(SysOrgUserEntity orgUser:orgUserList){
	    				SysUserEntity sysUserEntity = new SysUserEntity();
		    				sysUserEntity.setUserId(orgUser.getUserId());
		    				sysUserEntity.setStatus(AptitudeEnum.aptitudeStatus.JY.getKey());
	    				sysUserService.updateById(sysUserEntity);
	    			}
	    		}
	    		
	    	}else if(AptitudeEnum.aptitudeStatus.JY.getKey().equals(orgStatus)){
	    		org.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
	    		sysOrgService.update(org);
	    		Map<String, Object> params = new HashMap<String, Object>();
	    		params.put("orgNo", org.getOrgNo());
	    		params.put("accountType", SystemEnum.accountType.ORG.getKey());
	    		List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	    		if(orgUserList != null && orgUserList.size()>0){
	    			for(SysOrgUserEntity orgUser:orgUserList){
	    				SysUserEntity sysUserEntity = new SysUserEntity();
	    					sysUserEntity.setUserId(orgUser.getUserId());
	    					sysUserEntity.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
    				  sysUserService.updateById(sysUserEntity);
	    			}
	    		}
	    	}
	    }
		return RestResponse.success();
	}

	/**
	 * 注册资质人员信息                            
	 */
	@ApiOperation(value = "注册资质人员信息", notes = "注册资质人员信息")
	@SysLog("注册资质人员信息")
	@PostMapping("/registerAptitudeUser")
	public RestResponse registerAptitudeUser(SysUserEntity user) {

		if(user != null && StringUtils.isNotBlank(user.getUserCard())){
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("userCard", user.getUserCard());
				params.put("notUserId", user.getUserId());
			List<SysUserEntity> userList = sysUserService.queryAll(params);
        	if(userList !=null && userList.size()>0){
        		return RestResponse.error("用户登录名/身份证号码不能重复");
        	}
        	Map<String, Object> userName = new HashMap<String, Object>();
	        	userName.put("userName", user.getUserName());
	        	params.put("notUserId", user.getUserId());
			List<SysUserEntity> userNameList = sysUserService.queryAll(params);
        	if(userNameList !=null && userNameList.size()>0){
        		return RestResponse.error("用户登录名/身份证号码不能重复");
        	}
			String userCard = user.getUserCard();
			user.setUserName(userCard);
			String userId = sysUserService.registerUser(user);
			return RestResponse.success().put("userId", userId);
		}else{
			return RestResponse.error("用户登录名/身份证号码不能为空");
		}
		
	}
	
	/**
	 * 查看资质人员基本信息                            
	 */
	@ApiOperation(value = "查看资质人员基本信息", notes = "查看资质人员基本信息")
	@SysLog("查看资质人员基本信息")
	@PostMapping("/displayAptitudeUser")
	public RestResponse displayAptitudeUser(String userId) {

		SysUserEntity userEntity = sysUserService.getById(userId);
		return RestResponse.success().put("user", userEntity);
	}
	
	/**
	 * 查看资质人员资质详情列表                           
	 */
	@ApiOperation(value = "查看资质人员资质详情列表", notes = "查看资质人员资质详情列表")
	@SysLog("查看资质人员资质详情列表")
	@PostMapping("/displayAptitudeByUserId")
	public RestResponse displayAptitudeByUserId(String userId) {
		
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("aptitudeStatus", "2");
		params.put("userId", userId);
		List<SysUserAptitudeEntity> list = sysUserAptitudeService.queryAll(params);
		return RestResponse.success().put("list", list);
	}
	
	/**
	 * 人员状态（启用/禁用）切换
	 */
	@ApiOperation(value = "人员状态（启用/禁用）切换", notes = "人员状态（启用/禁用）切换")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String")
    })
	@SysLog("人员状态（启用/禁用）切换")
	@PostMapping("/updateUserStatus")
	public RestResponse updateUserStatus(String userId) {

		SysUserEntity user = sysUserService.getById(userId);
		if(user != null){
			String userStatus = user.getStatus();   //查询目前人员状态
			if(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey().equals(userStatus)){
				user.setStatus(AptitudeEnum.aptitudeStatus.JY.getKey());
				sysUserService.updateById(user);
			}else if(AptitudeEnum.aptitudeStatus.JY.getKey().equals(userStatus)){
				user.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
				sysUserService.updateById(user);
			}
		}
		return RestResponse.success();
	}
	

	/**
	 * 资质人员同意/不同意 
	 */
	@ApiOperation(value = "资质人员同意/不同意 ", notes = "资质人员同意/不同意 ")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "employStatus", value = "聘用状态（0未开始1拒绝被聘用2同意被聘用3被拒绝聘用4发起聘用等待同意）", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "orgNo", value = "单位id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "reasonRef", value = "拒绝聘用原因", dataType = "String")
	})
	@SysLog("资质人员同意/不同意 ")
	@PostMapping("/userEmployOrg")
	public RestResponse userEmployOrg(String employStatus,String orgNo,String reasonRef) {

		
		if(StringUtils.isBlank(orgNo)){
			return RestResponse.error("单位Id-orgNo不能为空");
		}
		if(StringUtils.isBlank(employStatus)){
			return RestResponse.error("聘用关系-employStatus-不能为空");
		}
//		if("1".equals(employStatus)){
//			employStatus = UserEnum.employStatus.TYBPY.getKey();
//		}else if("0".equals(employStatus)){
//			employStatus = UserEnum.employStatus.JJBPY.getKey();
//		}
		
		SysUserEntity  user =  getUser();
		if(user != null && user.getUserId() != null && user.getUserCard() != null && orgNo != null){
			String userId = user.getUserId();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", user.getUserId());
			params.put("orgNo", orgNo);
			List<SysOrgUserEntity> orgUserlist = sysOrgUserService.queryAll(params);
			if(orgUserlist != null && orgUserlist.size()>0){
				SysOrgUserEntity orgUser = new SysOrgUserEntity();
					orgUser.setId(orgUserlist.get(0).getId());
					orgUser.setUserId(userId);
					orgUser.setEmployStatus(employStatus);
					orgUser.setUserCard(user.getUserCard());
					orgUser.setSeal(user.getSeal());
					orgUser.setProcesDate(new Date());
					orgUser.setOrgNo(orgNo);
					orgUser.setReasonRef(reasonRef);
				ValidatorUtils.validateEntity(orgUser, AddGroup.class);
				sysOrgUserService.updateById(orgUser);
			}else{
				SysOrgUserEntity orgUser = new SysOrgUserEntity();
					orgUser.setUserId(userId);
					orgUser.setEmployStatus(employStatus);
					orgUser.setUserCard(user.getUserCard());
					orgUser.setSeal(user.getSeal());
					orgUser.setProcesDate(new Date());
					orgUser.setOrgNo(orgNo);
					orgUser.setReasonRef(reasonRef);
				sysOrgUserService.save(orgUser);
			}
		}else{
			return RestResponse.error();
		}
		return RestResponse.success();
	}

	
	/**
	 * 资质人员专用查看列表
	 */
	@ApiOperation(value = "资质人员专用查看列表", notes = "资质人员专用查看列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "身份证号码", dataType = "String")
    })
	@SysLog("资质人员专用查看列表")
	@PostMapping("/searchQualiUser")
	public RestResponse searchQualiUser(String userCard) {
		
		
		String orgNo = "";
		SysUserEntity  user =  getUser();
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());//登录人
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();
				}
			}else{
				return RestResponse.error("请检查该登陆用户是否是企业账号");
			}
		}
		
		
        if(StringUtils.isBlank(userCard)){
        	return RestResponse.error("身份证号码不能为空，请输入身份证号码");
        }
        Map<String, Object> params = new HashMap<String, Object>();
			params.put("userCard", userCard);
			params.put("accountType", "3");
			params.put("aptitudeStatus", "2");
		
		List<SysUserEntity> userList = sysUserService.queryUserByOrgNo(params);
		if(userList != null && userList.size()>0){
			for(SysUserEntity sysUserEntity:userList){
				Map<String, Object> userParams = new HashMap<String, Object>();
				userParams.put("userId", sysUserEntity.getUserId());
				userParams.put("aptitudeStatus","2");
				List<SysUserAptitudeEntity> userAptitudeList = sysUserAptitudeService.queryAll(params);
				if(userAptitudeList != null && userAptitudeList.size()>0){
					sysUserEntity.setUserAptitudeList(userAptitudeList);
				}else{
					return RestResponse.error("此人还未有合格资质");
				}
			}
		}else{
			return RestResponse.error("此身份证号码有误,请输入正确身份证号码");
		}
		
		
		Map<String, Object> orgUserParams = new HashMap<String, Object>();
			orgUserParams.put("userCard", userCard);
			orgUserParams.put("orgNo", orgNo);
			orgUserParams.put("notEqEmployStatus", "3");
		List<SysUserEntity> orgUserList = sysUserService.queryUserByOrgNo(orgUserParams);
		if(orgUserList != null && orgUserList.size()>0){
			return RestResponse.error("该人员已聘请成功,请到在职人员列表查看");
		}
		
		
		
		
		return RestResponse.success().put("list", userList);
	}
	
	/**
	 * 单位账号查看-聘用的资质人员
	 */
	@ApiOperation(value = "单位账号查看-聘用的资质人员", notes = "单位账号查看-聘用的资质人员")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "身份证号码", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "employStatus", value = "聘用状态", dataType = "String")
    })
	@SysLog("单位账号查看-聘用的资质人员")
	@PostMapping("/qualiUserList")
	public RestResponse qualiUserList(Integer currPage,Integer limit,String realName,String userCard,String employStatus,HttpServletRequest request) {

		  String orgNo = "";
		  SysUserEntity  user = this.getUser();
		  if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();
				}
			}
		  }else{
			  return RestResponse.error("请登录企业账号");
		  }

			Map<String, Object> paramsTemp = new HashMap<String, Object>();
			
				if("5".equals(employStatus)){
					List<String> employStatusList = new ArrayList<String>();
					employStatusList.add("1");
					employStatusList.add("4");
					paramsTemp.put("employStatusList", employStatusList);
				}else{
					paramsTemp.put("employStatus", employStatus);
				}
				paramsTemp.put("page", PageUtil.getCurrPage(currPage));
				paramsTemp.put("limit", PageUtil.getLimit(limit));
				paramsTemp.put("orgNo", orgNo);
				paramsTemp.put("realName", realName);
				paramsTemp.put("userCard", userCard);
				paramsTemp.put("accountType", "3");//资质人员
			Page page = sysOrgUserService.queryPage(paramsTemp);
			return RestResponse.success().put("page", page);
	}
	
	
	/**
	 * 待聘请资质人员列表-(有资质人员)-至少有一项资质是已通过的状态
	 */
	@ApiOperation(value = "待聘请资质人员列表-(有资质的人员)", notes = "待聘请资质人员列表-(有资质人员)")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String")
    })
	@SysLog("待聘请资质人员列表-(有资质人员)")
	@PostMapping("/dQualiUserList")
	public RestResponse dQualiUserList(Integer currPage,Integer limit,String realName) {

		
		  SysUserEntity  user = this.getUser();
		  if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgUserList.get(0).getOrgNo();
				}
			}
		  }else{
			  return RestResponse.error("请登录企业账号");
		  }
		
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("realName", realName);
			params.put("aptitudeStatus", AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
			params.put("accountType", "3");  //专家类型
//			Page page = sysOrgUserService.queryPage(params);
		    Page page = sysUserService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	

	/**
	 * 资质人员查看发起聘用单位列表
	 */
	@ApiOperation(value = "资质人员查看发起聘用单位列表", notes = "资质人员查看发起聘用单位列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号码", dataType = "String")
    })
	@SysLog("资质人员查看发起聘用单位列表")
	@PostMapping("/qualiOrgList")
	public RestResponse qualiOrgList(Integer currPage,Integer limit,String userCard) {

		String userId = "";
		SysUserEntity  user =  getUser();
		if(user != null && user.getUserId() != null){
			userId = user.getUserId();
		}
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userId", userId);
			params.put("userCard", userCard);
		Page page = sysOrgUserService.queryPage(params);
		return RestResponse.success().put("page", page);
	}

	
	/**
	 * 标记id(用于邮箱验证)
	 */
	@ApiOperation(value = "邮箱验证标记", notes = "邮箱验证标记")
	@ApiImplicitParams({
	      @ApiImplicitParam(paramType = "query", name = "enSignId", value = "加密标记id(用于邮箱验证)", dataType = "String")
	})
	@PostMapping("/verification")
	public RestResponse verification(String enSignId) {
		RestResponse result =null;
		String userName = "";
		try {
			String deSign = EncryUtils.decrypt(enSignId); //翻译成原文
			if(deSign != null && !"".equals(deSign)){
				String[] userNameArr = deSign.split("_");
				if(userNameArr != null && userNameArr.length>0){
					userName = userNameArr[0];
				}
			}
			SysUserEntity user=new SysUserEntity();
			user = sysUserService.queryByUserName(userName);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", user.getUserId());
			
//			List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
			boolean falg = true;             //查询是否是资质人员（资质人员邮箱验证后直接是启用状态其他则为待审核状态）
//			List<SysRoleEntity> list = sysRoleService.queryRoleList(user.getUserId());
//			if(list != null && list.size()>0){
//				for(SysRoleEntity role:list){
//					if(SystemEnum.roleType.ZZRY.getName().equals(role.getRoleName())){
//						falg = false;
//						break;
//					}
//				}
//			}
			
			if(falg == true && user != null){
				String orgNo = user.getOrgNo();
				if(orgNo != null && !"".equals(orgNo)){
					SysOrgEntity org = sysOrgService.getById(orgNo);
					if(org != null){ 
						//机构和用户均为待批准状态
						org.setStatus(AptitudeEnum.aptitudeStatus.YBSDPZ.getKey());
						user.setStatus(AptitudeEnum.aptitudeStatus.YBSDPZ.getKey());
						
						sysOrgService.update(org);
						sysUserService.updateById(user);
						result= RestResponse.success();
						result.put("result", userName);
					}
				 //校验资质人员
				}else{
					result = RestResponse.error();
				}
			}else if(falg == false){
				user.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());
				sysUserService.updateById(user);
				result= RestResponse.success();
				result.put("result", userName);
			}else{
				result = RestResponse.error();
			}
		} catch (Exception e) {
			result = RestResponse.error();
		}
		return result;
	}
}
